package com.sy.notice.util;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * @author 舒祥
 * @date 2022-01-25
 * @desc
 */
public class HttpClientUtil {


    private static final int TIME_OUT = 60000;

    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(TIME_OUT)
            .setConnectTimeout(TIME_OUT)
            .setConnectionRequestTimeout(TIME_OUT)
            .build();

    public String doGet(String url) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            HttpGet httpget = new HttpGet(url);
            httpget.setConfig(requestConfig);
            response = httpclient.execute(httpget);
            int statusCode = response.getStatusLine().getStatusCode();
            if (HttpStatus.SC_OK == statusCode) {
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity, "UTF-8");
                return result;
            } else {
                throw new RuntimeException("http get 请求失败，返回状态码 != 200 : " + statusCode);
            }
        } finally {
            if (null != response) {
                EntityUtils.consumeQuietly(response.getEntity());
            }
        }
    }


    public byte[] doJsonPostBytes(String url, Object params) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setConfig(requestConfig);
            //addHeaders(headers, httpPost);
            String jsonString = JSON.toJSONString(params);
            StringEntity requestEntity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
            httpPost.setEntity(requestEntity);
            response = httpclient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (HttpStatus.SC_OK == statusCode) {
                HttpEntity entity = response.getEntity();
                byte[] bytes = EntityUtils.toByteArray(entity);
                return bytes;
            } else {
                throw new RuntimeException("http post 请求失败，返回状态码 != 200 : " + statusCode);
            }
        } finally {
            if (null != response) {
                EntityUtils.consume(response.getEntity());
            }
        }
    }
}
